'ui';
let isRunning = false
let wd = null


let date = new Date()
if (date.getUTCMonth() + 1 >= 8 && date.getDate() >= 30) {
	toast('请付款后使用')
	exit()
}

threads.start(function () {
	// 检测是否打开无障碍权限
	if (!floaty.checkPermission()) {
		floaty.requestPermission()
		exit()
	}
	sleep(1000)
	wd = floaty.rawWindow(
		<vertical w="*" h="*">
			<frame layout_weight="1" h="30" layout_gravity="center">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="start"
					h="30"
					text="开始运行"
					textSize="12sp"
					gravity="center"
					padding="15 0 "
				/>
			</frame>
			<frame h="30" layout_weight="1" layout_gravity="center" marginTop="2">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="stop"
					h="30"
					text="结束运行"
					textSize="12sp"
					gravity="center"
					padding="15 0"
				/>
			</frame>
		</vertical>
	)
	wd.setPosition(40, 120)
	wd.exitOnClose()
	wd.stop.on('click', function () {
		isRunning = false
		exit()
		toastLog('运行结束')
	})
	wd.start.on('click', function () {
		//程序开始运行之前判断无障碍服务
		if (auto.service == null) {
			toast('请先开启无障碍服务！')
			return
		}

		console.log(isRunning);
		if (isRunning) {
			toastLog('运行暂停')
			isRunning = false
			ui.run(function () {
				wd.start.setText('开始运行')
			})
			// threads.shutDownAll()
			return
		}

		isRunning = true
		ui.run(function () {
			wd.start.setText('暂停运行')
		})
		threads.start(function () {
			toastLog('运行开始')
			sleep(1000)
			// toastLog('按音量键上,退出脚本。')
			// app.launchApp('微信')
			while (true) {
				if (isRunning) { 
					main()
				}
			}
		})
	})
})

ui.statusBarColor('#000000')
ui.layout(
	<vertical>
		<appbar bg="#000000">
			<toolbar title="预约排队('淘宝：保图工作室')" />
		</appbar>
		<Switch
			id="floatx"
			text="悬浮窗权限"
			checked="{{floaty.checkPermission()}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
		<Switch
			id="autoService"
			text="无障碍服务"
			checked="{{auto.service != null}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
	</vertical>
)

ui.floatx.on('click', function (checked) {
	floaty.requestPermission()
})

ui.autoService.on('check', function (checked) {
	// 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
	if (checked && auto.service == null) {
		app.startActivity({
			action: 'android.settings.ACCESSIBILITY_SETTINGS',
		})
	}
	if (!checked && auto.service != null) {
		auto.service.disableSelf()
	}
})

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on('resume', function () {
	// 此时根据无障碍服务的开启情况，同步开关的状态
	ui.floatx.checked = floaty.checkPermission()
	ui.autoService.checked = auto.service != null
})

function main() { 
	if (!text('可预约业务列表').exists()) { 
		return
	}

	let haveP = false
	textMatches('剩余:.*?').find().forEach(item => { 
		let tv = item.text()
		//console.log(tv.match(/剩余:(\d+)/));
		if (tv != '剩余:0') { 
			console.log(tv);
			haveP = true
			toastLog('有票')
			tv.parent().parent().click()
			let bounds = tv.bounds()
			click(bounds.centerX,bounds.centerY())
			click(bounds.centerX,bounds.centerY())
			sleep(200)
			return
		}
	})

	if (!haveP) { 
		toastLog('没票')
	}

	className('android.widget.ImageView').clickable().findOne(1000).click()
	text('刷新').findOne(2000).parent().click()
	while (!className('android.widget.ProgressBar').exists()) {
	}
	console.log('加载');
	while (className('android.widget.ProgressBar').exists()) {
	}
	console.log('ok');

}
